<template>
  <div class="ranking" style="height: 368px"></div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize";
export default {
  mixins: [resize],
  props: {
    type: {
      type: String,
      default: "1",
    },
    chartData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
       this.chart.on("click", function (params) {
        console.log(params);
        // window.open('https://www.baidu.com/s?wd=' + encodeURIComponent(params.name));
      });
    },
    setOptions(data) {
      let count = 0;
      this.chartData.forEach((item) => {
        count += item.value;
      });
      this.chart.setOption({
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        legend: {
          data: [
            "已逾期",
            "未开始",
            "进行中",
            "审批中",
            "逾期完成",
            "按时完成",
          ],
          top: "20",
          left: "center",
          itemGap: 20,
          itemWidth: 40,
          itemHeight: 14,
          textStyle: {
            color: "#8C8C8C",
          },
        },
        xAxis: {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#666",
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
          },
        },
        yAxis: {
          type: "category",
          data: [
            "赵克强",
            "刘颜星",
            "王明睿",
            "李坤",
            "张超",
            "李成峰",
            "刘伟奇",
          ],
          axisLine: {
            show: false,
            lineStyle: {
              color: "#666",
            },
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        dataZoom: [
          {
            show: true,
            type: "slider",
            brushSelect: false,
            fillerColor: "#f1f1f1",
            borderColor: "transparent",
            fillerColor: "rgba(167,183,204,0.4)",
            endValue: 4,
            filterMode: "empty",
            width: "10",
            top: "center",
            right: 10, //右边的距离
            zoomLoxk: true,
            yAxisIndex: 0,
            start: 0,
            startValue: 0,
            showDetail: false,
          },
          {
            type: "inside",
            yAxisIndex: 0,
            zoomOnMouseWheel: false, //滚轮是否触发缩放
            moveOnMouseMove: true, //鼠标滚轮触发滚动
            moveOnMouseWheel: true,
          },
        ],
        series: [
          {
            name: "已逾期",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(253,148,159)",
            },
            data: [320, 302, 301, 334, 390, 330, 320],
          },
          {
            name: "未开始",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(170,159,248)",
            },
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: "进行中",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(97,202,247)",
            },
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: "审批中",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(247,156,94)",
            },
            data: [120, 132, 101, 134, 90, 230, 210],
          },
          {
            name: "逾期完成",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(234,141,240)",
            },
            data: [220, 182, 191, 234, 290, 330, 310],
          },
          {
            name: "按时完成",
            type: "bar",
            stack: "总量",
            barWidth: 20,
            label: {
              normal: {
                show: false,
                position: "insideRight",
              },
            },
            itemStyle: {
              color: "RGB(119,225,186)",
            },
            data: [150, 212, 201, 154, 190, 330, 410],
          },
        ],
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>